package com.bawei.module_main.ui

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_common.arouter.ARouterActivityPath
import com.bawei.library_common.mvvm.BaseActivity
import com.bawei.module_main.R
import com.bawei.module_main.bean.CustomBean
import com.bawei.module_main.databinding.MainActivityGuideBinding
import com.bawei.module_main.viewholder.CustomPagerViewHolder
import com.blankj.utilcode.util.SPUtils
import com.zhpan.bannerview.constants.TransformerStyle
import com.zhpan.indicator.enums.IndicatorSlideMode

/**
 * 广告页
 */
@Route(path = ARouterActivityPath.PAGER_GUIDE)
class GuideActivity : BaseActivity<MainActivityGuideBinding>() {
    private val resList = listOf<Int>(R.drawable.guide0,R.drawable.guide1,R.drawable.guide2)
    private val titleList = listOf<String>("在这里\n你可以听到周围人的心声", "在这里\nTA会在下一秒遇见你", "在这里\n不再错过可以改变你一生的人")
    private val data = ArrayList<CustomBean>()//数据源
    init{
        for(i in 0..2){//for循环遍历
            data.add(CustomBean(resList[i],titleList[i]))
        }
    }
    override fun getLayout(): Int {
        return R.layout.main_activity_guide
    }

    override fun initData() {
        viewDataBinding.viewpager.apply {
            setAutoPlay(false)
            setCanLoop(false)
            setScrollDuration(1000)//换一次时间
            //设置动画效果
            setPageTransformerStyle(TransformerStyle.ROTATE)
            //指示器设置
            setIndicatorMargin(0,0,0,resources.getDimension(com.bawei.library_common.R.dimen.dp_100).toInt())//指示器位置
            setIndicatorColor(ContextCompat.getColor(this@GuideActivity,R.color.main_white),ContextCompat.getColor(this@GuideActivity,R.color.main_white_alpha_75))//设置指示器颜色：选中和未选中
            setIndicatorGap(resources.getDimension(com.bawei.library_common.R.dimen.dp_10).toInt())
            setIndicatorSlideMode(IndicatorSlideMode.WORM)//指示器切换样式
            setIndicatorRadius(resources.getDimension(com.bawei.library_common.R.dimen.dp_3).toInt(), resources.getDimension(
                com.bawei.library_common.R.dimen.dp_4_5).toInt())//指示器大小：选中和未选中
            //监听
            setOnPageChangeListener(object : ViewPager.OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int,
                ) {

                }

                override fun onPageSelected(position: Int) {
                    updateUI(position)
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })
            //数据
            setHolderCreator {CustomPagerViewHolder()}
            create(data)

        }
        //默认选中第一个
        updateUI(0)
    }
    //切换UI效果
    private fun updateUI(position: Int) {
        viewDataBinding.tvTitle.text = titleList[position]
        val translationAnim = ObjectAnimator.ofFloat(viewDataBinding.tvTitle, "translationX", -120f, 0f)
        translationAnim.duration = 2000
        translationAnim.interpolator = DecelerateInterpolator()//减速差值器
        val alphaAnimator1 = ObjectAnimator.ofFloat(viewDataBinding.tvTitle, "alpha", 0f, 1f)
        alphaAnimator1.duration = 2000
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(translationAnim, alphaAnimator1)
        animatorSet.start()
        //最后一个且是隐藏
        if (position == titleList.size - 1 && viewDataBinding.tvStart.visibility == View.GONE) {
            viewDataBinding.tvStart.visibility = View.VISIBLE
            //按钮动画效果：透明
            ObjectAnimator
                .ofFloat(viewDataBinding.tvStart, "alpha", 0f, 1f)
                .setDuration(2000).start()
        } else {
            viewDataBinding.tvStart.visibility = View.GONE
        }
    }

    override fun initListener() {
        //点击跳转到广告页面
        viewDataBinding.tvStart.setOnClickListener {
            ARouter.getInstance().build(ARouterActivityPath.PAGER_MAIN).navigation()
            SPUtils.getInstance().put("go",true)//记录已经走过广告页
        }
    }

}